﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.API.Framework;
using Sealight.Data;
using Sealight.API.Ioc;
using Sealight.Core.Domain;
using Sealight.Core.DataAdapter;
namespace Sealight.CoreAPI
{
   public class ArchiveService: IArchiveService
    {
        #region IArchiveService Members

        public archive AddOrUpdate(archive data)
        {
            if (data == null)
                throw new ArgumentNullException("Archive Entity is NULL.");
            //persists
            #region start_unit_of_work
            IUnitOfWork uow = null;
            if (!UnitOfWork.IsStarted)
                uow = UnitOfWork.Start();
            else uow = UnitOfWork.Current;
            #endregion
            bool commitTrans = false;
            if (!uow.IsInActiveTransaction)
            {
                //if not in active transaction
                //then begin transaction and commit it later
                uow.BeginTransaction(); //start transaction
                commitTrans = true;
            }

            try
            {
                 IArchiveDataRepository repo = IocContainer.Container().Resolve<IArchiveDataRepository>();

                 ArchiveEntity entity = repo.GetById(data.id);

                 if (entity == null)
                     entity = new ArchiveEntity();

                 entity.FileURL = data.file_url;
                 entity.Description = data.description;
                 entity.ExtensionType = data.extension_type;
                 entity.Title = data.title;
                 entity.CreatedDate = data.created_date;
                 entity.UpdatedDate = data.updated_date;
                 entity.IsActive = data.is_active;

                 repo.SaveOrUpdate(entity);

                if (commitTrans)
                    uow.TransactionalFlush();

                data = entity.ToDataAdapter();

            }
            catch (Exception ex)
            {
                //log for net
                throw new Exception("Error saving user data. ErrMsg: " + ex.Message, ex);
            }

            return data;
        }

        public archive GetById(Guid id)
        {
            if (id == Guid.Empty)
                throw new ArgumentNullException("Archive ID cannot be NULL.");

            ArchiveEntity data = null;
            try
            {
                #region start_unit_of_work
                IUnitOfWork uow = null;
                if (!UnitOfWork.IsStarted)
                    uow = UnitOfWork.Start();
                else uow = UnitOfWork.Current;
                #endregion
                IArchiveDataRepository repo = IocContainer.Container().Resolve<IArchiveDataRepository>();
                data = repo.GetById(id);
            }
            catch (Exception ex)
            {
                throw new Exception("Get Archive Error::" + ex.Message, ex);
            }

            if (data != null)
                return data.ToDataAdapter();

            return null;
        }

        public void Delete(archive data)
        {
            //persists
            #region start_unit_of_work
            IUnitOfWork uow = null;
            if (!UnitOfWork.IsStarted)
                uow = UnitOfWork.Start();
            else uow = UnitOfWork.Current;
            #endregion
            bool commitTrans = false;
            if (!uow.IsInActiveTransaction)
            {
                //if not in active transaction
                //then begin transaction and commit it later
                uow.BeginTransaction(); //start transaction
                commitTrans = true;
            }

            try
            {
                IArchiveDataRepository repo = IocContainer.Container().Resolve<IArchiveDataRepository>();
                ArchiveEntity entity = repo.GetById(data.id);

                if (entity != null)
                    repo.Delete(entity);

                if (commitTrans)
                    uow.TransactionalFlush();
            }
            catch (Exception ex)
            {
                //log for net
                throw new Exception("Error saving user data. ErrMsg: " + ex.Message, ex);
            }
        }

        public IList<archive> GetAll()
        {
            IList<ArchiveEntity> listData = null;
            try
            {
                #region start_unit_of_work
                IUnitOfWork uow = null;
                if (!UnitOfWork.IsStarted)
                    uow = UnitOfWork.Start();
                else uow = UnitOfWork.Current;
                #endregion
                IArchiveDataRepository repo = IocContainer.Container().Resolve<IArchiveDataRepository>();

                listData = repo.GetAll();
            }
            catch (Exception ex)
            {
                throw new Exception("Error get role data. ErrMsg: " + ex.Message, ex);
            }

            IList<archive> dataAdapter = null;

            if (listData != null)
            {
                dataAdapter = new List<archive>();

                foreach (ArchiveEntity entity in listData)
                {
                    dataAdapter.Add(entity.ToDataAdapter());
                }
            }

            return dataAdapter.OrderBy(x=>x.created_date).ToList();
        }

        #endregion
    }
}
